/*
 * @Author: Kang
 * @Date: 2022-02-12 18:10:37
 * @Last Modified by: Kang
 * @LastEditTime: 2022-02-19 20:22:04
 */
module.exports = app => {
    const Sequelize = require('sequelize');
    class NewsService extends app.Service {
        * index(request,id) {
            const Op = Sequelize.Op
            try {
                let current = parseInt(request.current) || 1;
                let pageSize = parseInt(request.pageSize) || 10;
                let where = {};
                let titles = request.title;
                if(titles) {
                    where.title = {
                        [Op.like]: '%' + titles + '%'
                    }
                }
                where.user_id = id
                console.log('where',where)
                const result = yield this.ctx.model.News.findAndCountAll({
                    order: [['id','DESC']],
                    where: where,
                    offset: (current -1) *pageSize,
                    limit: pageSize
                })
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * getListAll(request) {
            try {
                let current = parseInt(request.current) || 1;
                let pageSize = parseInt(request.pageSize) || 6;
                let where = {};
                where.user_id = request.user_id
                console.log('where',where)
                const result = yield this.ctx.model.News.findAndCountAll({
                    order: [['id','DESC']],
                    where: where,
                    offset: (current -1) *pageSize,
                    limit: pageSize
                })
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * create(request) {
            try {
                // console.log('request',request)
                 
                const news = yield this.ctx.model.News.create(request);
                const result = {};
                result.data = news;
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * update(request){
            try {
                const news = yield this.ctx.model.News.findByPk(request.id);
                const { title, text, img_url, type, home_img  } = request;
                console.log('title, text, img_url',title, text, img_url)
                const res = yield news.update({
                    title,
                    text,
                    img_url,
                    type,
                    home_img
                });
                const result = {};
                result.data = res;
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * destroy(id) {
            try {
                const news = yield this.ctx.model.News.findByPk(id);
                const res = yield news.destroy();
                const result = {};
                result.data = res;
                return result;
            } catch (error) {
                throw new Error(error);
            }
        }

        * webNews(request) {
            const param = {}
            if (request.type != null){
                param.type=request.type;
            }
            if (request.user_id != null){
                param.user_id=request.user_id;
            }
            const res = yield this.ctx.model.News.findAll({ where: param });
            return res;
        }

        * getNewData(request) {
            const Op = Sequelize.Op
            try {
                let where = {};
                let preWhere = {};
                let nextWhere = {};
                let data = {}
                const { id, user_id} = request
                where.id = id
                where.user_id = user_id
                preWhere.user_id = user_id
                nextWhere.user_id = user_id
                preWhere.id = {
                    [Op.gt]: id
                }
                nextWhere.id = {
                    [Op.lt]: id
                }
                const result = yield this.ctx.model.News.findOne({
                    where: where,
                })
                const nextRes = yield this.ctx.model.News.findOne({
                    where: preWhere,
                    order: [['id','ASC']],
                })
                const pevRes = yield this.ctx.model.News.findOne({
                    where: nextWhere,
                    order: [['id','DESC']],
                })
                data.currData = result;
                data.preData = pevRes;
                data.nextData = nextRes;
                return data;
            } catch (error) {
                throw new Error(error);
            }
        }

        * getNewInfo(request) {
            console.log(request.id)
            const res = yield this.ctx.model.News.findOne({'where':{id:request.id}})
            return res;
        }

        * getLatelyNews(request) {
            let where = {};
            let count = parseInt(request.count) || 5
            where.user_id = request.user_id
            const res = yield this.ctx.model.News.findAll({
                where: where,
                limit: count,
                order: [['create_time','DESC']],
            })
            return res;
        }
    }
    return NewsService;
} 